<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
  <el-select v-model="appletInfo"
             filterable
             clearable
             value-key="appletId"
             :placeholder="placeholder"
             @change="changeType">
    <el-option v-for="appletInfo in appletList" :key="appletInfo.appletId" :label="appletInfo.appletName"
               :value="appletInfo"/>
  </el-select>
</template>

<script>
  export default {
    name: "MessageAppletSelect",
    props: ['value', 'placeholder','typeTags'],
    created() {
      this.loadAppletList()
    },
    data() {
      return {
        appletInfo: null,
        appletList: []
      }
    },
    methods: {
      changeType() {
        this.$emit('input', this.appletInfo.appletId)
        this.$emit('change', this.appletInfo)
      },
      // 获取应用注册数据
      loadAppletList() {
        const url = '/joys-rest/joys-master-data/applets'
        this.$http.get(url,{params:{typeTags:this.typeTags,admin:true}}).then(resp => {
          this.appletList = resp.data
          this.mapAppletInfo()
        }, resp => {
          this.$message.error(resp.data)
        })
      },
      mapAppletInfo() {
        this.appletList && this.appletList.find(appletInfo => {
          if (appletInfo.appletId == this.value) {
            this.appletInfo = appletInfo
            this.$emit('matched', this.appletInfo)
            return true
          } else {
            return false
          }
        })
      }
    },
    watch:{
      typeTags(){
        this.loadAppletList()
      }
    }
  }
</script>

<style scoped>

</style>
